package com.love_weinuo.ui;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;

import com.tencent.imsdk.TIMManager;
import com.tencent.qcloud.uikit.TUIKit;
import com.tencent.qcloud.uikit.common.IUIKitCallBack;
import com.love_weinuo.R;
import com.love_weinuo.databinding.ActivityMainBinding;
import com.love_weinuo.ui.activity.LoginActivity;
import com.love_weinuo.ui.fragment.ApplicationState;
import com.love_weinuo.ui.fragment.BaseFragmentAdapter;
import com.love_weinuo.ui.fragment.IndexFragment;
import com.love_weinuo.ui.fragment.MainFragmentProxy;
import com.love_weinuo.ui.fragment.MySelfFragment;
import com.love_weinuo.ui.fragment.SecondFragment;
import com.love_weinuo.ui.view.MainTableView;
import com.love_weinuo.util.LogUtil;
import com.love_weinuo.util.SpUtils;
import com.love_weinuo.util.rx.RxActivityTool;

public class MainActivity extends BaseActivity implements ViewPager.OnPageChangeListener, MainTableView.OnTableChangedListener {

    ActivityMainBinding mainBinding;
    private ApplicationState applicationState = new ApplicationState();
    private int currentAt = 0;
    private Fragment fragment[] = new Fragment[3];
    String userSign = (String) SpUtils.get("module_user_sign", "");
    String phone = (String) SpUtils.get("module_user_name", "");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        mainBinding.setActivity(this);
        initView();
    }

    private void initView() {
        add(this);
        SpUtils.put("module_is_first", "yes");
        SpUtils.put("isLogin", "yes");
        BaseFragmentAdapter fragmentAdapter = new BaseFragmentAdapter(getSupportFragmentManager());
        fragment[0] = new IndexFragment();
        fragment[1] = new SecondFragment();
        fragment[2] = new MySelfFragment();
        fragmentAdapter.addData(fragment);
        mainBinding.viewPager.setAdapter(fragmentAdapter);
        mainBinding.viewPager.setCurrentItem(currentAt);
        mainBinding.viewPager.addOnPageChangeListener(this);
        mainBinding.mainTableView.setOnTableChangedListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rightText:
                break;
            default:
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (mainBinding.mainTableView != null) {
            mainBinding.mainTableView.onClick(mainBinding.viewPager.getCurrentItem());
        }
        if (applicationState.isMainSliding() && position == 1) {
            applicationState.setMainSliding(false);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (!applicationState.isMainSliding() && state != ViewPager.SCROLL_STATE_IDLE) {
            applicationState.setMainSliding(true);
        }
    }

    @Override
    public void onTableChanged(int index) {
        if (currentAt != index) {
            currentAt = index;
            mainBinding.viewPager.setCurrentItem(index, false);
        } else {
            if (fragment[index] instanceof MainFragmentProxy) {
                ((MainFragmentProxy) fragment[index]).scrollToTop();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (TextUtils.isEmpty(phone) && TextUtils.isEmpty(userSign)) {
            RxActivityTool.skipActivityAndFinish(MainActivity.this, LoginActivity.class);
        }
        String loginUser = TIMManager.getInstance().getLoginUser();
        if (!phone.equals(loginUser)) {
            imLogin(phone, userSign);
        } else {
            LogUtil.e(TIMManager.getInstance().getLoginUser() + "仍在登录中");
        }
    }

    private void imLogin(String account, final String userSig) {
        TUIKit.login(account, userSig, new IUIKitCallBack() {
            @Override
            public void onSuccess(Object data) {
                LogUtil.e("registerIM 登录成功");
            }

            @Override
            public void onError(String module, int errCode, String errMsg) {
                imLogin(phone, userSign);
            }
        });
    }

}
